/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mbeanserver;

import java.io.IOException;
import java.net.Inet4Address;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

/**
 *
 * @author alan
 */
public class MBServer {

    private MBeanServer MBServer;
    private JMXServiceURL JMXURL;
    private JMXConnectorServer JMXConn;
    private MBeanServerConnection JMXSC;

    public JMXConnectorServer getJMXConn() {
        return JMXConn;
    }

    public JMXServiceURL getJMXURL() {
        return JMXURL;
    }

    public MBeanServer getMBServer() {
        return MBServer;
    }

    public MBServer() throws RemoteException, IOException {
        try {
            LocateRegistry.createRegistry(17503);
        } catch (RemoteException e) {
            System.out.println("Não foi possível registrar a porta 17503: " + e.getMessage());
        }
        System.out.println("Inicializando MBean Server");
        this.MBServer = MBeanServerFactory.createMBeanServer("tcc");
        ConnRMI();
        // makeHTMLAdapter();

    }

    public void ConnRMI() throws IOException {
        this.JMXURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:17503/server");
        this.JMXConn = JMXConnectorServerFactory.newJMXConnectorServer(this.JMXURL, null, this.MBServer);
        System.out.println("MBeans registrados");
        this.JMXConn.start();
    }

    public static void main(String[] args) {
        try {
            new MBServer();
        } catch (RemoteException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MBServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
